<?php

namespace App\Evaluation\Controllers;

use App\Extensions\Content;
use App\Extensions\Form;
use Encore\Admin\Controllers\AuthController as BaseAuthController;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\Hash;

class AuthController extends BaseAuthController
{
    protected $loginView = 'admin.login';

    protected function settingForm()
    {
        $class = config('admin.database.users_model');

        $form = new Form(new $class());

        $form->display('username', trans('admin.username'));
        $form->text('name', trans('admin.name'))->rules('required');
//        $form->image('avatar', trans('admin.avatar'));
        $form->password('password', trans('admin.password'))->rules('confirmed|required');
        $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
            ->default(function ($form) {
                return $form->model()->password;
            });
        $form->text('bank_account', __('银行账号'));
        $form->text('bank', __('开户银行'));
        $form->setAction(admin_url('auth/setting'));

        $form->ignore(['password_confirmation']);

        $form->saving(function (Form $form) {
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = Hash::make($form->password);
            }
        });

        $form->saved(function () {
            admin_toastr(trans('admin.update_succeeded'));

            return redirect(admin_url('auth/setting'));
        });

        return $form;
    }

    public function myInfo(Content $content)
    {
        $user = Admin::user();
        $class = config('admin.database.users_model');
        $form = new Form(new $class());
        $form->setActionCurrent()->isLayer();
        $form->setTitle(' ');
        $form->display('company', '机构名称');
        $form->text('name', __('负责人'))->required();
        $form->mobile('mobile', __('电话'))->required();
        $form->textarea('major', __('优势领域'));
        $form->textarea('about', __('机构简介'))->rows(15);
        $form->text('address', __('地址'));
        $form->saved(function (Form $form) {
            admin_toastr('操作成功');
            return back();
        });
        if (!request()->isMethod('GET')) {
            return $form->update($user['id']);
        }
        return $content->title('信息备案')->body($form->edit($user['id']));
    }

}
